package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.constants.MessageConstant;
import com.huike.dto.DictTypePagDto;
import com.huike.entity.Clue;
import com.huike.entity.SysDictType;
import com.huike.mapper.SysDictTypeMapper;
import com.huike.result.PageResult;
import com.huike.result.Result;
import com.huike.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class DictServiceImpl implements DictService {
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Override
    public Result listDictType(DictTypePagDto dto) {
        if (dto == null) {
            return Result.error(MessageConstant.DATA_NOT_EXIST);
        }
        if (dto.getPageSize() == null || dto.getPage()==null) {
                dto.setPage(1);
                dto.setPageSize(10);
        }
        IPage<SysDictType> pageResult = new Page<>(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<SysDictType> lqw = new LambdaQueryWrapper<>();
        lqw.like(dto.getDictType() != null, SysDictType::getDictType, dto.getDictType());
        lqw.like(dto.getDictName() != null, SysDictType::getDictName, dto.getDictName());
        lqw.eq(dto.getStatus() != null, SysDictType::getStatus, dto.getStatus());
        if (dto.getBeginTime() != null && dto.getEndTime() != null) {
            lqw.le(SysDictType::getCreateTime, dto.getEndTime()).ge(SysDictType::getCreateTime, dto.getBeginTime());
        }
        sysDictTypeMapper.selectPage(pageResult, lqw);

        return Result.success(new PageResult(pageResult.getTotal(), pageResult.getRecords()));
    }

    @Override
    public Result createDictType(SysDictType sysDictType) {
        if (sysDictType == null) {
            return Result.error(MessageConstant.DATA_NOT_EXIST);
        }
        sysDictType.setCreateTime(LocalDateTime.now());
        //TODO:createBy字段
        sysDictTypeMapper.insert(sysDictType);
        return Result.success();
    }

    @Override
    public Result findById(Long id) {
        SysDictType sysDictType = sysDictTypeMapper.selectById(id);
        return Result.success(sysDictType);
    }

    @Override
    public Result updateDictType(SysDictType sysDictType) {
        sysDictTypeMapper.updateById(sysDictType);
        return Result.success();
    }

    @Override
    public Result deleteById(Long id) {
        if (id == null) {
            return Result.error(MessageConstant.DATA_NOT_EXIST);
        }
        sysDictTypeMapper.deleteById(id);
        return Result.success();
    }


}
